package com.ecplatform.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain;

@Configuration
@EnableWebFluxSecurity  // 启用 Spring WebFlux 安全 不能用 @EnableWebSecurity -> 这个是 Spring MVC 的安全
@Slf4j
public class GatewaySecurityConfig {

    /**
     * 基于 Spring MVC，则使用 HttpSecurity。
     * 基于 Spring WebFlux，则应使用 ServerHttpSecurity。
     */

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        log.error("--------------进入 GatewaySpringSecurity------------------");
        http
                .csrf(ServerHttpSecurity.CsrfSpec::disable) // 禁用 CSRF
                .authorizeExchange(exchange -> exchange
                        .anyExchange().permitAll() // 允许所有请求
                );

        return http.build();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
